﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using VmcRemote.Common;

namespace VmcRemote.PpcRemote
{
    public partial class SettingsForm : Form
    {
        public RemoteSettings Settings { get; set; }

        public SettingsForm()
        {
            InitializeComponent();
        }

        private void SettingsForm_Load(object sender, EventArgs e)
        {
            foreach (Type t in RemoteSettings.ConnectionTypes)
                this.connectionTypeList.Items.Add(DescriptionAttribute.GetTypeDescription(t));
            if (connectionTypeList.Items.Count > 0) connectionTypeList.SelectedIndex = 0;
            foreach (Type t in RemoteSettings.ServerTypes)
                this.serverTypeList.Items.Add(DescriptionAttribute.GetTypeDescription(t));
            if (serverTypeList.Items.Count > 0) serverTypeList.SelectedIndex = 0;

            RemoteSettings settings = RemoteSettings.Load();
            if (settings != null)
            {
                this.hostTextBox.Text = settings.Host;
                this.portTextBox.Text = settings.Port.ToString();
                this.connectionTypeList.Text = DescriptionAttribute.GetTypeDescription(settings.ConnectionType);
                this.serverTypeList.Text = DescriptionAttribute.GetTypeDescription(settings.ServerType);
            }
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            RemoteSettings settings;
            if (Validate(out settings))
            {
                settings.Save();
                this.Settings = settings;
                this.Close();
            }
            else
            {
                MessageBox.Show("Connection Failed!");
            }
        }

        private void testButton_Click(object sender, EventArgs e)
        {
            RemoteSettings settings;
            if (Validate(out settings))
                MessageBox.Show("Connected Successfully!");
            else
                MessageBox.Show("Connection Failed!");
        }

        private bool Validate(out RemoteSettings settings)
        {
            settings = null;

            string hostName = hostTextBox.Text;
            Type connectionType = SelectedConnectionType;
            Type serverType = SelectedServerType;
            int portNumber = 0;
            try { portNumber = int.Parse(portTextBox.Text); }
            catch
            {
                MessageBox.Show("Please specify a valid port number");
                return false;
            }
            if (string.IsNullOrEmpty(hostName))
            {
                MessageBox.Show("Please specify a valid host name");
                return false;
            }
            if (connectionType == null)
            {
                MessageBox.Show("Please specify a connection type");
                return false;
            }
            if (serverType == null)
            {
                MessageBox.Show("Please specify a server type");
                return false;
            }

            settings = new RemoteSettings
            {
                Host = hostName,
                Port = portNumber,
                ConnectionType = connectionType,
                ServerType = serverType,
                ResourceManager = VmcRemote.PpcRemote.Properties.Resources.ResourceManager
            };
            return settings.Validate();
        }

        private void textBox_GotFocus(object sender, EventArgs e)
        {
            inputPanel1.Enabled = true;
        }

        private void textBox_LostFocus(object sender, EventArgs e)
        {
            inputPanel1.Enabled = false;
        }

        private Type SelectedConnectionType
        {
            get
            {
                Type selection = null;
                if (connectionTypeList.SelectedIndex != -1)
                {
                    selection = RemoteSettings.ConnectionTypes
                        .Where(t => DescriptionAttribute.GetTypeDescription(t) == (string)connectionTypeList.SelectedItem)
                        .First();
                }
                return selection;
            }
            set
            {
                var descr = DescriptionAttribute.GetTypeDescription(value);
                for (int i = 0; i < connectionTypeList.Items.Count; i++)
                {
                    var type = (string)connectionTypeList.Items[i];
                    if (type == descr)
                        connectionTypeList.SelectedIndex = i;
                }
            }
        }

        private Type SelectedServerType
        {
            get
            {
                Type selection = null;
                if (serverTypeList.SelectedIndex != -1)
                {
                    selection = RemoteSettings.ServerTypes
                        .Where(t => DescriptionAttribute.GetTypeDescription(t) == (string)serverTypeList.SelectedItem)
                        .First();
                }
                return selection;
            }
        }

        private void serverTypeList_SelectedValueChanged(object sender, EventArgs e)
        {
            var selected = SelectedServerType;
            Type t = DefaultConnectionAttribute.GetDefaultConnection(selected);
            if (t != null)
                SelectedConnectionType = t;
            int port = DefaultPortAttribute.GetDefaultPort(selected);
            if (port > 0)
                portTextBox.Text = port.ToString();
        }
    }
}